/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package burp;

import java.util.Set;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author adetlefsen
 */
public class VersionsComponent extends javax.swing.JPanel {

    BurpExtender extender;
    IBurpExtenderCallbacks callbacks;
    DefaultListModel domainsListModel = new DefaultListModel();
    DefaultListModel versionsListModel = new DefaultListModel();

    /**
     * Creates new form VersionsComponent
     */
    public VersionsComponent(final IBurpExtenderCallbacks callbacks) {
        initComponents();

        extender = (BurpExtender) BurpExtender.getInstance();
        this.callbacks = callbacks;

        //get the found list of versions
        Set<String> domains = extender.versions.keySet();
        addDomains(domains);

        //add a listener to the domains list
        domainsList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent evt) {
                callbacks.printOutput("value changed!");
                String selected = domainsList.getSelectedValue();
                callbacks.printOutput("selected domain: " + selected);
                Set<String> versions = extender.versions.get(selected);
                callbacks.printOutput("found versions: " + versions);
                addVersions(versions);
            }
        });
    }

    protected void addDomains(Set<String> newDomains) {
        for (String domain : newDomains) {
            domainsListModel.addElement(domain);
        }
    }

    protected void addDomain(String newDomain) {
        domainsListModel.addElement(newDomain);
    }

    protected void removeDomain(int index) {
        domainsListModel.remove(index);
        //domainsList.setModel(domainsListModel);
    }

    protected void clearDomains() {
        domainsListModel.clear();
    }

    protected void addVersions(Set<String> newVersions) {
        if (newVersions != null) {
            versionsListModel.clear();
            for (String version : newVersions) {
                versionsListModel.addElement(version);
            }
        }
    }

    protected void addVersion(String newVersion) {
        versionsListModel.addElement(newVersion);
    }

    protected void clearVersions() {
        versionsListModel.clear();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        domainsList = new javax.swing.JList<>();
        jScrollPane2 = new javax.swing.JScrollPane();
        versionsList = new javax.swing.JList<>();
        jLabel7 = new javax.swing.JLabel();
        buttonClearDomain = new javax.swing.JButton();
        buttonClearCache = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(229, 137, 0));
        jLabel1.setText("Software Versions Discovered");

        jLabel6.setText("Domain");

        domainsList.setModel(domainsListModel);
        domainsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(domainsList);

        versionsList.setModel(versionsListModel);
        jScrollPane2.setViewportView(versionsList);

        jLabel7.setText("Versions");

        buttonClearDomain.setText("Clear Domain Cache");
        buttonClearDomain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClearDomainActionPerformed(evt);
            }
        });

        buttonClearCache.setText("Clear Entire Cache");
        buttonClearCache.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClearCacheActionPerformed(evt);
            }
        });

        jLabel5.setText("To reset the list of found versions you can clear the cache:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonClearDomain)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonClearCache))
                    .addComponent(jLabel5))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonClearDomain)
                    .addComponent(buttonClearCache))
                .addContainerGap(44, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonClearDomainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearDomainActionPerformed
        //get the selected domain
        String domain = domainsList.getSelectedValue();
        int index = domainsList.getSelectedIndex();

        //clear the cache for the selected domain
        extender.clearCache(domain);

        //clear the UI component
        clearVersions();
        callbacks.printOutput("Removing domain at: " + index);
        domainsList.setSelectedIndex(-1);
        removeDomain(index);
        domainsList.repaint();
        domainsList.updateUI();
    }//GEN-LAST:event_buttonClearDomainActionPerformed

    private void buttonClearCacheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearCacheActionPerformed
        extender.clearCache();

        //clear the UI component
        clearVersions();
        clearDomains();
    }//GEN-LAST:event_buttonClearCacheActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonClearCache;
    private javax.swing.JButton buttonClearDomain;
    private javax.swing.JList<String> domainsList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList<String> versionsList;
    // End of variables declaration//GEN-END:variables
}
